package com.bosch.in.rtp1.ept.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.bosch.in.rtp1.ept.constant.CommonConstants;
import com.bosch.in.rtp1.ept.constant.LotStatusColumnName;
import com.bosch.in.rtp1.ept.dao.LotStatusDao;
import com.bosch.in.rtp1.ept.model.dto.LotStatus;
import com.bosch.in.rtp1.ept.model.technical.SearchCriteria;

/**
 * Concrete JDBC implementation of LotStatusDao. Provide basic CRUD action and various searching on LotStatus table.
 * @author TPR1HC
 *
 */
public class LotStatusJDBCDao extends GenericDao implements LotStatusDao {
	
	private static final Logger LOGGER = Logger.getLogger(LotStatusJDBCDao.class);
	
	/**
	 * Constructor to inject connection
	 * @param con	DB connection
	 */
	public LotStatusJDBCDao(Connection con) {
		super(con);
	}
	
	@Override
	public List<LotStatus> getLotStatus(SearchCriteria searchCriteria) throws SQLException{
		LOGGER.debug("Begin LotStatusJDBCDao.getLotStatus");
		List<LotStatus> lotStatuses = new ArrayList<LotStatus>();
		
		// Execute query to load LotStatus list according to user-specified search criteria
		ResultSet resultSet = executeQuery(searchCriteria.getSqlQuery(), searchCriteria.getParameterValues().toArray());           
		DecimalFormat df = new DecimalFormat(CommonConstants.NUMBER_FORMAT);
		while (resultSet.next()) {
			// save result set to LotStatus object
			LotStatus lotStatus = new LotStatus();
			lotStatus.setLotId(resultSet.getString(LotStatusColumnName.LOT_ID_DB));
			lotStatus.setCurMainQTY(resultSet.getInt(LotStatusColumnName.CURMAIN_QTY_DB));
			lotStatus.setPartId(resultSet.getString(LotStatusColumnName.PART_ID_DB));
			lotStatus.setLotType(resultSet.getString(LotStatusColumnName.LOT_TYPE_DB));
			lotStatus.setCustomerName(resultSet.getString(LotStatusColumnName.CUSTOMER_NAME_DB));
			lotStatus.setPriority(resultSet.getInt(LotStatusColumnName.PRIORITY_DB));
			lotStatus.setState(resultSet.getString(LotStatusColumnName.STATE_DB));
			lotStatus.setLotComment(resultSet.getString(LotStatusColumnName.LOT_COMMENT_DB));
			lotStatus.setHoldCode(resultSet.getString(LotStatusColumnName.HOLD_CODE_DB));
			lotStatus.setHoldReason(resultSet.getString(LotStatusColumnName.HOLD_REASON_DB));
			lotStatus.setProdArea(resultSet.getString(LotStatusColumnName.PRO_AREA_DB));
			lotStatus.setLocation(resultSet.getString(LotStatusColumnName.LOCATION_DB));
			lotStatus.setCurPrcdId(resultSet.getString(LotStatusColumnName.CUR_PRCD_ID_DB));
			lotStatus.setStage(resultSet.getString(LotStatusColumnName.STAGE_DB));
			lotStatus.setRecipeId(resultSet.getString(LotStatusColumnName.RECIPE_ID_DB));
			lotStatus.setRecipeTitle(resultSet.getString(LotStatusColumnName.RECIPE_ID_DB));
			lotStatus.setRecipeTitle(resultSet.getString(LotStatusColumnName.RECIPE_TITLE_DB));
			lotStatus.setEqpType(resultSet.getString(LotStatusColumnName.EQP_TYPE_DB));
			lotStatus.setEquipID(resultSet.getString(LotStatusColumnName.EQUIP_ID_DB));
			lotStatus.setQueueTime(resultSet.getTimestamp(LotStatusColumnName.QUEUE_TIME_DB));
			lotStatus.setStartTime(resultSet.getTimestamp(LotStatusColumnName.START_TIME_DB));
			lotStatus.setCr1Sap(resultSet.getString(LotStatusColumnName.CR1_SAP_DB));
			lotStatus.setWaitTime(resultSet.getFloat(LotStatusColumnName.WAIT_TIME_DB));
			float round_XPlan = Float.valueOf(df.format(resultSet.getFloat(LotStatusColumnName.X_PLAN_DB)));
			lotStatus.setX_plan(round_XPlan);
			float round_XRW = Float.valueOf(df.format(resultSet.getFloat(LotStatusColumnName.X_RW_DB)));
			lotStatus.setX_rw(round_XRW);
			float round_XRWH = Float.valueOf(df.format(resultSet.getFloat(LotStatusColumnName.X_RWH_DB)));
			lotStatus.setX_rwh(round_XRWH);
			lotStatuses.add(lotStatus);
		} 
		// close statement, result set
		resultSet.close();
		close();
		LOGGER.debug("End LotStatusJDBCDao.getLotStatus");
		return lotStatuses;
	}
}
